<template>
  <div>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="query"
      :params="fetchParams"
      :datakey="'rows'"
      height="300"
      :onRowSelectChange="columns => (this.partColumns = columns)"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="columns => (this.str = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="deleteTable">删除</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="addpart" :title="'配件选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addpartModel @close="closeHandler" @add="addColumns" />
    </BaseDialog>
    <div style="text-align:center;">
      <el-button size="small" type="primary" @click="addpart = true">新增</el-button>
      <el-button size="small" type="primary" @click="saveMessage">保存</el-button>
      <el-button size="small" type="primary" @click="closePage">取消</el-button>
    </div>
  </div>
</template>

<script>
import { queryRo, maintainRoRepairPart } from '@/api/repair/repairAssign/repairAssign';
import addpartModel from './addpartModel';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'ItemPartModel',
  components: {
    addpartModel
  },
  mixins: [dictionary],
  props: ['roNo'],
  data() {
    this.partTable = null;
    return {
      fetchParams: {},
      str: [],
      partColumns: [],
      itemId: '',
      addpart: false,
      columns: this.createTableColumns()
    };
  },
  mounted() {
    this.partTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库',
          dataIndex: 'storageName',
          sorter: true,
          filter: true,
          filterType: 'input',
          itemList: []
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单位',
          dataIndex: 'unitCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件成本单价',
          dataIndex: 'partCostPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '成本金额',
          dataIndex: 'partCostAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件销售金额',
          dataIndex: 'partSalesAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '收费区分',
          dataIndex: 'chargePartitionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    async query() {
      return await queryRo(this.roNo[0].RO_NO);
    },
    async saveMessage() {
      let json = {
        tablelist: this.str,
        roNo: this.roNo[0].RO_NO,
        itemId: this.itemId
      };
      let res = await maintainRoRepairPart(json);
      if (res.resultCode == 200) {
        this.closePage();
      } else {
        this.$notify.error({ title: '提示信息', message: '保存失败！' });
      }
    },
    deleteTable() {
      this.partColumns.forEach(x => {
        if (x.ITEM_ID) this.itemId += x.ITEM_ID + ',';
      });
      if (this.partColumns.length > 0) {
        this.partTable.EXECUTE_DELETE();
      } else {
        this.$notify.error({ title: '提示信息', message: '请先选择数据!' });
      }
    },
    addColumns(val) {
      this.partTable.EXECUTE_INSERT(val);
      this.addpart = false;
    },
    closeHandler() {
      this.addpart = false;
    },
    closePage() {
      this.$emit('close');
    }
  }
};
</script>
